home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / amitcp2_x_gcc.lha / getopt.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  2KB  |  91 lines

  1. /*
  2.    **   @(#)getopt.c    2.5 (smail) 9/15/87
  3.  */
  4.  
  5. /*
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9.  * directly from AT&T.  The people there assure me that it is indeed
  10.  * in the public domain.
  11.  *
  12.  * There is no manual page.  That is because the one they gave out at
  13.  * UNIFORUM was slightly different from the current System V Release 2
  14.  * manual page.  The difference apparently involved a note about the
  15.  * famous rules 5 and 6, recommending using white space between an option
  16.  * and its first argument, and not grouping options that have arguments.
  17.  * Getopt itself is currently lenient about both of these things White
  18.  * space is allowed, but not mandatory, and the last option in a group can
  19.  * have an argument.  That particular version of the man page evidently
  20.  * has no official existence, and my source at AT&T did not send a copy.
  21.  * The current SVR2 man page reflects the actual behavor of this getopt.
  22.  * However, I am not about to post a copy of anything licensed by AT&T.
  23.  */
  24.  
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28.  
  29. /*LINTLIBRARY */
  30. #define EOF    (-1)
  31. #define ERR(s, c)\
  32.   if(opterr) { fprintf(stderr, "%s%s%lc\n", argv[0], s, c); }
  33.  
  34. int opterr = 1;
  35. int optind = 1;
  36. int optopt;
  37. char *optarg;
  38.  
  39. int
  40. getopt (int argc, char *const *argv, const char *opts)
  41. {
  42.   static int sp = 1;
  43.   register long int c;
  44.   register char *cp;
  45.  
  46.   if (sp == 1)
  47.     if (optind >= argc ||
  48.     argv[optind][0] != '-' || argv[optind][1] == '\0')
  49.       return (EOF);
  50.     else if (strcmp (argv[optind], "--") == NULL)
  51.       {
  52.     optind++;
  53.     return (EOF);
  54.       }
  55.   optopt = c = argv[optind][sp];
  56.   if (c == ':' || (cp = index (opts, c)) == NULL)
  57.     {
  58.       ERR (": illegal option -- ", c);
  59.       if (argv[optind][++sp] == '\0')
  60.     {
  61.       optind++;
  62.       sp = 1;
  63.     }
  64.       return ('?');
  65.     }
  66.   if (*++cp == ':')
  67.     {
  68.       if (argv[optind][sp + 1] != '\0')
  69.     optarg = &argv[optind++][sp + 1];
  70.       else if (++optind >= argc)
  71.     {
  72.       ERR (": option requires an argument -- ", c);
  73.       sp = 1;
  74.       return ('?');
  75.     }
  76.       else
  77.     optarg = argv[optind++];
  78.       sp = 1;
  79.     }
  80.   else
  81.     {
  82.       if (argv[optind][++sp] == '\0')
  83.     {
  84.       sp = 1;
  85.       optind++;
  86.     }
  87.       optarg = NULL;
  88.     }
  89.   return (c);
  90. }
  91.